#include <iostream>

using namespace std;

//macro constant
#define number 1024

void constVariable()
{
    //const variable still is variable
    //but read only
    //const int a;  error -> is inlegal,must init when declare
    const int a = 10;
    //a = 20;
    
    cout << "const int a = " << a << endl;
    cout << "#define number = " << number << endl;
}

void constPointer()
{
    int a = 10;
    int b = 100;

    const int *p = &a;//const to point
    //*p = b; error
    p = &b;

    int const *p1 = &a;//const to point
    //*p1 = b; error
    p1 = &b;

    int *const p2 = &a;//point to const
    *p2 = b;
    //p2 = &b; error

    const int x = 20;
    //int *p3 = &x; error
    const int *p3 = &x;


    cout << *p << endl;
    cout << *p1 << endl;
    cout << *p2 << endl;
    cout << *p3 << endl;

    //总结：const在*左边，即为指向常量的指针，不能通过指针改变其指向的值，但是可以改变这个指针的指向。
    //总结：const在*右边，即为常量指针，不能改变这个指针的指向，但是可以通过指针改变其指向的值。
}

int main()
{
    constVariable();
    constPointer();
    return 0;
}

